package com.quik.web.product.mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.quik.backend.mapper.BaseDao;
import com.quik.entity.DtaProduct;
import com.quik.entity.DtaProductCategory;
import com.quik.entity.DtaProductSubCategory;

/**
 * 功能说明:
 *         产品业务逻辑的数据持久化类
 * @author Jenny
 *
 */
@Repository
public class ProductDao extends BaseDao<DtaProduct>{
	/**
	 * 查询分类的总数
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	public int findCategoryCount(String categoryName) throws Exception{
		int count = (Integer)this.get("findCategoryCount", (Object)categoryName);
		return count;
	}
	
	/**
	 * 根据商品名称查询商品列表
	 * @param categoryName
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List findCategory(String categoryName, int intPage, int pageSize) throws Exception{
		Map map = new HashMap(3);
		map.put("categoryName", categoryName == null?null:categoryName.trim());		
		map.put("currPage", (intPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		
		List list = load("findCategory", map);
		
		return list;
	}
	
	/**
	 * 根据商品大类ID查询商品子类列表
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public List findSubCategory(Integer[] ids) throws Exception{
		Map map = new HashMap(1);
		map.put("array", ids);
		
		List list = load("findSubCategory", map);
		
		return list;
	}
	
	/**
	 * 查询商品分类
	 * @param categoryName
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List findCategory() throws Exception{
		List list = this.loadAll("findCategoryItem");
		return list;
	}
	
	/**
	 * 根据商品大类查询商品总数
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	public int findCategoryCountByCategoryName(String categoryName) throws Exception{
		int count = (Integer)this.get("findCategoryCountByCategoryName", (Object)categoryName);
		return count;
	}
	
	/**
	 * 新增商品大类
	 * @param category
	 * @throws Exception
	 */
	public void addCategory(DtaProductCategory category) throws Exception{
		add("addCategory", category);
	}
	
	/**
	 * 根据商品子类查询商品总数
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	public int findSubCategoryCountBySubCategoryName(String subCategoryName, Integer categoryId) throws Exception{
		Map map = new HashMap(2);
		map.put("subCategoryName", subCategoryName);
		map.put("categoryId", categoryId);
		
		int count = (Integer)this.get("findSubCategoryCountBySubCategoryName", map);
		return count;
	}
	
	/**
	 * 新增商品子类
	 * @param category
	 * @throws Exception
	 */
	public void addSubCategory(DtaProductSubCategory subCategory) throws Exception{
		add("addSubCategory", subCategory);
	}
	
	/**
	 * 根据条件查询商品总数
	 * @param categoryId
	 * @param subCategoryId
	 * @param productNo
	 * @param productName
	 * @return
	 * @throws Exception
	 */
	public int findProductCount(Integer categoryId, Integer subCategoryId, String productNo, 
			String productName) throws Exception{
		Map map = new HashMap(4);
		map.put("categoryId", categoryId);
		map.put("subCategoryId", subCategoryId);
		map.put("productNo", productNo == null ? null : productNo.trim());
		                       
		map.put("productName", productName == null ? null : productName.trim());
		
		int count = (Integer)this.get("findProductCount", map);
		return count;
	}
	
	/**
	 * 根据条件查询商品列表
	 * @param categoryId
	 * @param subCategoryId
	 * @param productNo
	 * @param productName
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List<DtaProduct> findProduct(Integer categoryId, Integer subCategoryId, String productNo, String productName, 
			Integer intPage, Integer pageSize) throws Exception{
		Map map = new HashMap(6);
		map.put("categoryId", categoryId);
		map.put("subCategoryId", subCategoryId);
		map.put("productNo", productNo == null ? null : productNo.trim());
		map.put("productName", productName == null ? null : productName.trim());
		map.put("currPage", (intPage - 1) * pageSize);
		map.put("pageSize", pageSize);
		
		List<DtaProduct> list = load("findProduct", map);
		
		return list;
	}
	
	/**
	 * 根据类别ID查询子类列表
	 * @param categoryId
	 * @return
	 * @throws Exception
	 */
	public List findSubCategoryByCategoryId(Integer categoryId) throws Exception{
		List list = this.load("findSubCategoryByCategoryId", categoryId);
		return list;
	}
	
	/**
	 * 根据商品编号查询商品总数
	 * @param productNo
	 * @return
	 * @throws Exception
	 */
	public int findProductNoCount(String productNo) throws Exception{
		int count = (Integer)this.get("findProductNoCount", (Object)productNo);
		return count;
	}
	
	/**
	 * 新增商品
	 * @param product
	 * @throws Exception
	 */
	public void addProduct(DtaProduct product) throws Exception{
		add("addProduct", product);
	}

	public List<DtaProduct> findAllProduct() {
		return this.loadAll("findAllProduct");
	}

	public List<DtaProductCategory> findAllProductCategory() {
		return this.loadAll("findAllProductCategory");
	}

	public List<DtaProductSubCategory> findAllproductSubCategory() {
		return this.loadAll("findAllProductSubCategory");
	}
}
